

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>高度最大适应 - 数据表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">

  <!--jquery-->
  <script src="../../statics/libs/jquery.min.js"></script>
  <!--ztree-->
  <link rel="stylesheet" href="../../statics/plugins/ztree/css/zTreeStyle/zTreeStyle.css">
  <script src="../../statics/plugins/ztree/jquery.ztree.all.min.js"></script>

  <script src="../../js/common.js"></script>

</head>
<body>

<div class="layui-fluid">

  <div class="layui-row layui-col-space5">
    <div class="layui-col-xs3">
      <button id="addParent" class="layui-btn layui-btn-sm layui-icon layui-icon-add-1">添加</button>
      <button id="addLeaf" class="layui-btn layui-btn-sm " style="display: none"></button>
    </div>
    <div class="layui-col-xs2">
      <button id="edit" class="layui-btn layui-btn-sm">修改</button>
    </div>
    <div class="layui-col-xs2">
      <button id="up" class="layui-btn layui-btn-sm">上移</button>
    </div>
    <div class="layui-col-xs2">
      <button id="down" class="layui-btn layui-btn-sm">下移</button>
    </div>
    <div class="layui-col-xs2">
      <button id="remove" class="layui-btn layui-btn-sm">删除</button>
    </div>
    <div class="layui-col-xs1">
    </div>
  </div>

  <hr>
  <div class="layui-row">
    <div class="layui-col-md12">
      <div style="overflow-y: auto;max-height: 250px">
        <ul id="dataTree" class="ztree"></ul>
      </div>
    </div>
  </div>
</div>

  
  <script src="../../layuiadmin/layui/layui.js"></script>
  <script>
  layui.config({
    base: '../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var admin = layui.admin
    ,layer = layui.layer
    ,layerTips = parent.layer === undefined ? layui.layer : parent.layer //获取父窗口的layer对象
    ,$ = layui.jquery;



      //部门树------------start
      var data_ztree;
      var data_setting = {
          view: {
              selectedMulti: false,
              //showIcon: false
          },
          data: {
              simpleData: {
                  enable: true,
                  idKey: "deptId",
                  pIdKey: "parentId",
                  rootPId: -1
              },
              key: {
                  //url: "nourl"
                  //isParent:true
              },
              keep: {
                  parent:true,
                  //leaf:true
              }
          },
          check: {
              enable: false,
              nocheckInherit: true,
              chkboxType: {"Y": "", "N": ""}
          },
          callback: {
              onClick: zTreeOnClick,
              beforeDrag: beforeDrag,
              beforeRemove: beforeRemove,
              beforeRename: beforeRename,
              onRemove: onRemove
          },
          edit: {
              enable: true,
              showRemoveBtn: false,
              showRenameBtn: false
          }
      };

      function zTreeOnClick(event, treeId, treeNode) {
          console.log("id:  "+treeNode.deptId);
          console.log("name:   "+treeNode.name);
      };

      function getDataTree() {
          //加载菜单树
          Ajax.request({
              url: "../common/dept.js",
              async: true,
              successCallback: function (r) {
                  data_ztree = $.fn.zTree.init($("#dataTree"), data_setting, r.list);
                  //展开所有节点
                  data_ztree.expandAll(true);
              }
          });
      };

      getDataTree();


      //部门树------------end


      //修改树的方法-------start
      function beforeDrag(treeId, treeNodes) {
          return true;
      }
      function beforeRemove(treeId, treeNode) {
          console.log("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
          return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
      }
      function onRemove(e, treeId, treeNode) {
          console.log("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
      }
      function beforeRename(treeId, treeNode, newName) {
          if (newName.length == 0) {
              alert("节点名称不能为空.");
              var zTree = $.fn.zTree.getZTreeObj("dataTree");
              setTimeout(function(){zTree.editName(treeNode)}, 10);
              return false;
          }
          return true;
      }
      function getTime() {
          var now= new Date(),
              h=now.getHours(),
              m=now.getMinutes(),
              s=now.getSeconds(),
              ms=now.getMilliseconds();
          return (h+":"+m+":"+s+ " " +ms);
      }

      var newCount = 1;
      function add(e) {
          var zTree = $.fn.zTree.getZTreeObj("dataTree"),
              isParent = e.data.isParent,
              nodes = zTree.getSelectedNodes(),
              parentNode = nodes[0];//选择的节点设置为上级部门
          if(parentNode)
          {
            $('#add_pName').val(parentNode.name);
              $('#add_pId').val(parentNode.deptId);
            $("#add_pTid").val(parentNode.tId);
          }
          layer.open({
              type: 1,
              area: ['400px', '450px'],
              title: '添加部门',
              content: $('#addDialog') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
              ,btn: ['添加', '取消']
              ,yes: function(index, layero){
                  var //add_id = $("#add_id").val()
                      add_name = $("#add_name").val()
                      ,add_pId = $("#add_pId").val()
                      ,add_pTid = $("#add_pTid").val();

//                  if(add_id && add_id!='') {
                      if (add_name && add_name != '')
                      {
                          if (add_pId && add_pId != '')
                          {
                              //根据pid查找父节点
                              parentNode = zTree.getNodeByTId(add_pId);
                          }
                      }
//                  }
                      else {
                          alert("请填写部门名称");
                          return;
                      }

                  if(parentNode)
                  {
                    var treeNode = zTree.addNodes(parentNode, {deptId:(100 + (newCount++)), parentId:add_pId, name:add_name});
                    if (treeNode) {
                        zTree.updateNode(treeNode[0]);
                        $("#add_name").val('');
                        layer.close(index);
                    } else {
                        alert("叶子节点被锁定，无法增加子节点");
                    }
                  }else alert("请选择上级部门");

              }

          });

          /*if (treeNode) {
              treeNode = zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, isParent:isParent, name:"new node" + (newCount++)});
          } else {
              treeNode = zTree.addNodes(null, {id:(100 + newCount), pId:0, isParent:isParent, name:"new node" + (newCount++)});
          }
          if (treeNode) {
              zTree.editName(treeNode[0]);
          } else {
              alert("叶子节点被锁定，无法增加子节点");
          }*/
      };
      function edit() {
          var zTree = $.fn.zTree.getZTreeObj("dataTree"),
              nodes = zTree.getSelectedNodes(),
              treeNode = nodes[0];
          if (nodes.length == 0) {
              alert("请先选择一个节点");
              return;
          }
          zTree.editName(treeNode);
      };
      function remove(e) {
          var zTree = $.fn.zTree.getZTreeObj("dataTree"),
              nodes = zTree.getSelectedNodes(),
              treeNode = nodes[0];
          if (nodes.length == 0) {
              alert("请先选择一个节点");
              return;
          }
          var callbackFlag = $("#callbackTrigger").attr("checked");
          zTree.removeNode(treeNode, callbackFlag);
      };
      function clearChildren(e) {
          var zTree = $.fn.zTree.getZTreeObj("dataTree"),
              nodes = zTree.getSelectedNodes(),
              treeNode = nodes[0];
          if (nodes.length == 0 || !nodes[0].isParent) {
              alert("请先选择一个父节点");
              return;
          }
          zTree.removeChildNodes(treeNode);
      };

      function move(e)
      {
          var zTree = $.fn.zTree.getZTreeObj("dataTree"),
              type = e.data.type,
              nodes = zTree.getSelectedNodes()
          if (nodes.length == 0) {
              alert("请先选择一个节点");
              return;
          }
          var curNode = nodes[0],
              targetNode;

              if(type=='prev')
                targetNode = nodes[0].getPreNode();
              else if(type=='next')
                  targetNode = nodes[0].getNextNode();
              else
                  return;
          if(targetNode)
          {
              zTree.moveNode(targetNode, curNode, type);
          }
      }

      $(document).ready(function(){
          //$.fn.zTree.init($("#dataTree"), data_setting, zNodes);
          $("#addParent").bind("click", {isParent:true}, add);
          $("#addLeaf").bind("click", {isParent:false}, add);
          $("#edit").bind("click", edit);
          $("#remove").bind("click", remove);
          $("#clearChildren").bind("click", clearChildren);
          $("#up").bind("click", {type:'prev'}, move);
          $("#down").bind("click", {type:'next'}, move);

          //下拉选择数初始化
          Ajax.request({
              url: "../common/dept.js",
              async: true,
              successCallback: function (r) {
                  data_ztree = $.fn.zTree.init($("#treeSelect"), setting, r.list);
                  //展开所有节点
                  data_ztree.expandAll(true);
              }
          });

          $("#add_pName").bind("click", showMenu);
      });

      //-----------------下拉选择菜单
      var setting = {
          view: {
              dblClickExpand: false
          },
          data: {
              simpleData: {
                  enable: true,
                  idKey: "deptId",
                  pIdKey: "parentId",
                  rootPId: -1
              }
          },
          callback: {
              beforeClick: beforeClick,
              onClick: onClick
          }
      };

      function beforeClick(treeId, treeNode) {
          //var check = (treeNode && !treeNode.isParent);
          //if (!check) alert("只能选择城市...");
          //return check;
          return treeNode;
      }

      function onClick(e, treeId, treeNode) {
          var zTree = $.fn.zTree.getZTreeObj("treeSelect"),
              nodes = zTree.getSelectedNodes(),
              treeNode = nodes[0];
          $("#add_pName").val(treeNode.name);
          $("#add_pId").val(treeNode.deptId);
          $("#add_pTid").val(treeNode.tId);//treeNode节点唯一ID
      }

      function showMenu() {

          var pIdObj = $("#add_pName");
          var pIdOffset = $("#add_pName").offset();
          //$("#menuContent").css({left:pIdOffset.left + "px", top:pIdOffset.top + pIdObj.outerHeight() + "px"}).slideDown("fast");
          $("#menuContent").slideDown("fast");
          $("#tmpDiv").bind("mousedown", onBodyDown);
      };

      function hideMenu() {
          $("#menuContent").fadeOut("fast");
          $("#tmpDiv").unbind("mousedown", onBodyDown);
      }
      function onBodyDown(event) {
          if (!(event.target.id == "add_pName" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
              hideMenu();
          }
      }

      //修改树的方法-------end



    
  });


  function getDataTreeToArray(){
      var zTree = $.fn.zTree.getZTreeObj("dataTree");
      var nodes = zTree.transformToArray(zTree.getNodes());
      return nodes;
  }


  </script>
</body>

<div id="addDialog" style="display: none">
    <div id="tmpDiv">

      <!--<div class="layui-row" style="margin-top: 10px">
        <div class="layui-col-xs4">部门Id</div>
        <div class="layui-col-xs8">
          <input id="add_id" type="text" placeholder="请输入ID" class="layui-input">
        </div>
      </div>-->

      <div class="layui-row" style="margin-top: 10px">
        <div class="layui-col-xs4">部门名称</div>
        <div class="layui-col-xs8">
          <input id="add_name" type="text" placeholder="请输入名称" class="layui-input">
        </div>
      </div>
      <div class="layui-row" style="margin-top: 10px">
        <div class="layui-col-xs4">上级部门</div>
        <div class="layui-col-xs8">
          <input type="text" id="add_pName" placeholder="请选择上级部门" readonly class="layui-input">
          <input type="hidden" id="add_pId">
          <input type="hidden" id="add_pTid">
        </div>
      </div>

          <div id="menuContent" class="menuContent" style="display:none; position: absolute;">
            <ul id="treeSelect" class="ztree" style="margin-top:0; width:160px;"></ul>
          </div>
    </div>

</div>

</html>